﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;

namespace QuanLyDuocPham.Controllers.QuanLyHeThong.DanhMuc
{
    class NCC_Control : IDisposable
    {

        DuocPhamDataContext db = new DuocPhamDataContext(staticClass.Conn);
       
        public void Dispose()
        {
            if (db != null)
            {
                db.Dispose();
                db = null;
            }
        }
        public void loadComboBox(ComboBox cmb)
        {

            cmb.Items.Clear();
            var list = db.NCC_Get_List();
            var typeorder = list.OrderBy(w => w.TEN_NCC);
            foreach (var t in typeorder)
            {
                cmb.Items.Add(t);
            }
            cmb.SelectedValuePath = "MA_NCC";
            cmb.IsTextSearchEnabled = true;
            cmb.IsTextSearchCaseSensitive = false;
            cmb.IsEditable = true;
            NCC_Get_ListResult temp = new NCC_Get_ListResult();

            temp.MA_NCC = "...";
            
            temp.TEN_NCC = "Thêm mới";
            cmb.Items.Add(temp);
        }

        public string loadID_moi()
        {
            string ID = "";

            int rowcount = db.NCC_Get_List().Count();
            if (rowcount == 0)
            {
                ID = "NCC0001";
            }
            else
            {
                for (int i = 1; i < rowcount+2; i++)
                {
                    string IDtam = "NCC";
                    for (int j = 0; j < (4 - i.ToString().Length); j++)
                        IDtam = IDtam + "0";
                    IDtam = IDtam + i;
                    Boolean kq = true;
                    var data = db.NCC_Get_List();

                    foreach (var t in data)
                    {

                        if (t.MA_NCC.Trim() == IDtam.Trim())

                            kq = false;
                    }
                    if (kq == true)
                    {
                        ID = IDtam;
                        break;
                    }
                }

            }
            return ID;

        }


        public void addData(string ma_ncc, TextBox txtTen_NCC, CheckBox chbThuong_Xuyen, TextBox txtDia_Chi, TextBox txtTinh_TP, TextBox txtEmail, TextBox txtD_Thoai, TextBox txtFax, TextBox txtMST, TextBox txtNguoi_DD, TextBox txtTen_TK, TextBox txtSo_TK, TextBox txtTen_NganHang, TextBox txtDC_NganHang, RichTextBox txtGhi_Chu)
        {
            if (validateData( txtTen_NCC,  txtDia_Chi,  txtTinh_TP,  txtD_Thoai,  txtMST,  txtNguoi_DD)==false)
                MessageBox.Show("Chưa nhập đủ thông tin!");
            else
            {
               
                db.NCC_Insert( ma_ncc,  txtTen_NCC.Text,  chbThuong_Xuyen.IsChecked,  txtDia_Chi.Text,  txtTinh_TP.Text,  txtEmail.Text,  txtD_Thoai.Text,  txtFax.Text,  txtMST.Text,  txtNguoi_DD.Text,  txtTen_TK.Text,  txtSo_TK.Text,  txtTen_NganHang.Text,  txtDC_NganHang.Text,  txtGhi_Chu.Document.Blocks.ToString());
                MessageBox.Show("Đã thêm thành công");
            }
        }
        private bool validateData( TextBox txtTen_NCC, TextBox txtDia_Chi, TextBox txtTinh_TP, TextBox txtD_Thoai, TextBox txtMST, TextBox txtNguoi_DD)
        {
            if (txtTen_NCC.Text.Trim() == "" || txtDia_Chi.Text.Trim() == "" || txtTinh_TP.Text.Trim() == "" || txtD_Thoai.Text.Trim() == "" || txtMST.Text.Trim() == "" || txtNguoi_DD.Text.Trim() == "" )
            {
                return false;
            }

            return true;
        }

        public void loadData(ListView List)
        {
            List.Items.Clear();
            var list = db.NCC_Get_List();
            var typeorder = list.OrderBy(w => w.TEN_NCC);
            foreach (var t in typeorder)
            {
                List.Items.Add(t);
            }
        }

        public void loadData(DataPager dp)
        {
            
            var list = db.NCC_Get_List();
            var typeorder = list.OrderBy(w => w.TEN_NCC);
            ObservableCollection<Object> dpSource = new ObservableCollection<Object>();
            int i = 1;
            foreach (var t in typeorder)
            {
                if (t.TEN_NCC.Trim() != "")
                {
                    dpSource.Add(new { STT = i, t.DC_NGANHANG,  t.DIA_CHI, t.DIEN_THOAI, t.EMAIL, t.FAX, t.GHI_CHU, t.TEN_NCC, t.MA_NCC, t.MST, t.NGUOI_DD, t.SO_TK, t.TINH_TP, t.TEN_NGANHANG, t.TEN_TK, t.THUONG_XUYEN });
                    i++;
                }
            }
            dp.ItemsSource = dpSource;
            dp.ItemsPerPage = 10;
        }
        public void updateData(string ma_ncc, TextBox txtTen_NCC, CheckBox chbThuong_Xuyen, TextBox txtDia_Chi, TextBox txtTinh_TP, TextBox txtEmail, TextBox txtD_Thoai, TextBox txtFax, TextBox txtMST, TextBox txtNguoi_DD, TextBox txtTen_TK, TextBox txtSo_TK, TextBox txtTen_NganHang, TextBox txtDC_NganHang, RichTextBox txtGhi_Chu)
        {
            if(validateData( txtTen_NCC,  txtDia_Chi,  txtTinh_TP,  txtD_Thoai,  txtMST,  txtNguoi_DD))
            {
                db.NCC_Update(ma_ncc, ma_ncc, txtTen_NCC.Text, chbThuong_Xuyen.IsChecked, txtDia_Chi.Text, txtTinh_TP.Text, txtEmail.Text, txtD_Thoai.Text, txtFax.Text, txtMST.Text, txtNguoi_DD.Text, txtTen_TK.Text, txtSo_TK.Text, txtTen_NganHang.Text, txtDC_NganHang.Text, txtGhi_Chu.Document.Blocks.ToString());
                MessageBox.Show("Đã cập nhật!");
            }
            else
                MessageBox.Show("Chưa nhập đủ thông tin!");

            
        }

        public string deleteData(string ID_Delete, string error)
        {
            error = "";
            if (db.PHIEUNHAP_GetByMaNcc(ID_Delete).Count() > 0 || db.DUOCPHAM_GetByMaNcc(ID_Delete).Count() > 0)
                error = "Không thể xóa";
            else
            {
                db.NCC_Delete(ID_Delete);
                error = "Xóa thành công!";
            }
            return error;


        }

        public void loadForm_Edit(string ID_Edit, TextBox txtTen_NCC, CheckBox chbThuong_Xuyen, TextBox txtDia_Chi, TextBox txtTinh_TP, TextBox txtEmail, TextBox txtD_Thoai, TextBox txtFax, TextBox txtMST, TextBox txtNguoi_DD, TextBox txtTen_TK, TextBox txtSo_TK, TextBox txtTen_NganHang, TextBox txtDC_NganHang, RichTextBox txtGhi_Chu)
        {

            var sl = (NCC_GetByMaNccResult)db.NCC_GetByMaNcc(ID_Edit).Single();


            
            txtTen_NCC.Text = sl.TEN_NCC;
            chbThuong_Xuyen.IsChecked = sl.THUONG_XUYEN;
            txtDia_Chi.Text = sl.DIA_CHI;
            txtTinh_TP.Text = sl.TINH_TP;
            txtEmail.Text = sl.EMAIL;
            txtD_Thoai.Text = sl.DIEN_THOAI;
            txtFax.Text = sl.FAX;
            txtMST.Text = sl.MST;
            txtNguoi_DD.Text = sl.NGUOI_DD;
            txtSo_TK.Text = sl.SO_TK;
            txtTen_TK.Text = sl.TEN_TK;
            txtTen_NganHang.Text = sl.TEN_NGANHANG;
            txtDC_NganHang.Text = sl.DC_NGANHANG;
            txtGhi_Chu.AppendText( sl.GHI_CHU);

        }
    }
}
